﻿package javase.socket.client;
import java.net.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ChatClient4 extends JApplet implements Runnable{
		JTextField tf_username;
		JTextField tf_message;
		JTextArea ta_msg;
		java.awt.List lst_users;
		JButton btn_conn;
		JButton btn_send;
		JButton btn_close;
		JButton btn_clear;
		Choice cmb_users;
		Socket s=null;
		DataInputStream ips=null;
		DataOutputStream ops=null;
		String flag;
		String myname;
	public void init() {
		flag="";
		myname="";
		tf_username=new JTextField(10);
		tf_message=new JTextField(10);
		ta_msg=new JTextArea(10,10);
		ta_msg.setBackground(Color.PINK);
		ta_msg.setAutoscrolls(true);
		lst_users=new java.awt.List(10);
		btn_conn=new JButton("连接");
		btn_send=new JButton("发送");
		btn_close=new JButton("关闭");
		btn_clear=new JButton("清屏");
		cmb_users=new Choice();
		cmb_users.addItem("所有人");
		JPanel p1=new JPanel();
		JPanel p2=new JPanel();
		JPanel p3=new JPanel();
		JPanel p4=new JPanel();
		this.setLayout(new BorderLayout());
		p1.add(tf_username);p1.add(btn_conn);
		p2.add(cmb_users);p2.add(tf_message);
		p3.add(btn_send);p3.add(btn_close);p3.add(btn_clear);
		p4.setLayout(new GridLayout(2,0));
		p4.add(p2);p4.add(p3);
		add(p1,"North");
		add(ta_msg,"Center");
		add(lst_users,"East");
		add(p4,"South");
		tf_message.setEditable(false);
		ta_msg.setEditable(false);
		setSize(425,250);
		setVisible(true);
		btn_conn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e)
			{
				String name=tf_username.getText();
				if(name!="")
				{
					try {
						myname=name;
						flag="#a#"+myname+"#";
						ops.writeUTF(name);
						tf_message.setEditable(true);
						btn_conn.setEnabled(false);
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
			}
		});
		cmb_users.addItemListener(new ItemListener(){
			public void itemStateChanged(ItemEvent e)
			{
				if(cmb_users.getSelectedItem().toString().trim().equals("所有人"))
				{
					flag="#a#"+myname+"#";
				}
				else
				{
					flag="#p#"+myname+"#"+cmb_users.getSelectedItem().trim().toString()+"#";
				}
			}
		});
		btn_send.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e)
			{
				String b=tf_message.getText();
				tf_message.setText("");
				try{
					ops.writeUTF(flag+b);
				}
				catch(IOException e1){}
			}
		});
		tf_message.addKeyListener(new KeyAdapter(){
			public void keyPressed(KeyEvent e)
			{
    		   if(e.getKeyCode()==10)
    		   {
					String b=tf_message.getText();
					tf_message.setText("");
					try{
						ops.writeUTF(flag+b);
					}
					catch(IOException e1){}
    		   }
			}
		});
		btn_close.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e)
			{
				try{
					ops.writeUTF("#q#"+myname);
				}
				catch(IOException e1){}
			}
		});
		btn_clear.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e)
			{
				ta_msg.setText("");
			}
		});
		new Thread(this).start();
	}
	public void run()
	{
		try
		{
			s = new Socket("localhost",6001);
			ips=new DataInputStream(s.getInputStream());
			ops=new DataOutputStream(s.getOutputStream());
			boolean cRunning=true;
			while(cRunning)
			{
				String msg=ips.readUTF();
				if(msg.substring(0,3).equals("#@#"))
				{
					if(lst_users.getItemCount()!=0)
					{
						lst_users.removeAll();
						cmb_users.removeAll();
						cmb_users.addItem("所有人");
						msg=msg.substring(3);
						StringTokenizer stk=new StringTokenizer(msg,"#");
						while(stk.hasMoreTokens())
						{
							String name=stk.nextToken();
							lst_users.addItem(name);
							cmb_users.addItem(name);
						}
					}
					else
					{
						msg=msg.substring(3);
						StringTokenizer stk=new StringTokenizer(msg,"#");
						while(stk.hasMoreTokens())
						{
							String name=stk.nextToken();
							lst_users.addItem(name);
							cmb_users.addItem(name);
						}
					}
				}
				else if(msg.substring(0,3).trim().equals("#q#"))//"#q#"+myname+"#离开聊天室"
				{
					msg=msg.substring(3);
					String hisname=msg.substring(0,msg.indexOf("#"));
					msg=msg.substring(msg.indexOf("#")+1);
					if(hisname.equals(myname))
					{
						ta_msg.append(msg+'\n');
						lst_users.remove(hisname);
						cmb_users.remove(hisname);	
						ips.close();
						ops.close();
						s.close();
					//	btn_conn.setEnabled(true);
						return;
					//	System.exit(0);
					}
					else
					{
						lst_users.remove(hisname);
						cmb_users.remove(hisname);
						ta_msg.append(hisname+msg+'\n');
					}
				}
				else
				{
					ta_msg.append(msg+'\n');
				}
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}	
}
